package com.hht.tools.netlib

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.Coil
import coil.load
import coil.util.CoilUtils
import com.hht.baselibs.adapter.BaseDataAdapter
import com.hht.netlibs.NetWorkManagement
import com.hht.netlibs.NetWorkManagement.await
import com.hht.toolanduilibs.showLog
import com.hht.toolanduilibs.showToast
import com.hht.tools.R
import com.hht.tools.netlib.api.WebServiceApi
import com.hht.tools.netlib.bean.DataBean
import kotlinx.coroutines.*

class NetLibsUI : AppCompatActivity(), CoroutineScope by MainScope() {

    private var mWebServiceApi: WebServiceApi = NetWorkManagement.createServiceWithMySelf()
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mDataAdapter: BaseDataAdapter<DataBean>
    private var dataList = mutableListOf<DataBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.net_libs_ui)
        mRecyclerView = findViewById(R.id.net_recycle_view)
        mDataAdapter = BaseDataAdapter<DataBean>(R.layout.net_libs_ui_item) { mView, dataBean ->
            val nameTv = mView.findViewById<TextView>(R.id.net_tv_item)
            val titleIv=mView.findViewById<ImageView>(R.id.net_iv_item)
            nameTv.text = dataBean.title

        }.setAdapterData(dataList).setOnItemOnClickListener {
            it.author.showToast(this)
        }
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mDataAdapter
        launch {
            val mBannerData =
                withContext(Dispatchers.Default) {
                    mWebServiceApi.getBannerData().await()
                }.toString()
            mBannerData.showLog(1)

            val mArticleData =
                withContext(Dispatchers.Default) {
                    mWebServiceApi.getArticleData(1).await()
                }
            dataList.addAll(mArticleData.articleData.dataList)
            mDataAdapter.notifyDataSetChanged()
        }
    }
}